/* Copyright Andy Mitchell 2007. No warranties */

function ThreadListPopupUI(gtdinboxPage, elParent){
	this.gtdinboxPage = gtdinboxPage;
	this.doc = gtdinboxPage.doc;
	this.settings = gtdinboxPage.gtdinboxBrowser.settings;
	this._labels = gtdinboxPage._labels;
	this.threadList = gtdinboxPage._threadList;
	this.threadListUI = gtdinboxPage._threadListUI;
		
	this.eventObservers = new EventObservers(this);
		
	this.create(elParent);
	this.popuper = new Popuper( this.gtdinboxPage, this.el, this );
	this.popuper.addEventHandler( this, "popup", this.onPopup );
	this.popuper.addEventHandler( this, "popupOff", this.onPopupOff );
	
	this._labels.addEventHandler( this, "refresh-labels", this.labelsUpdate );
	
};

ThreadListPopupUI.prototype.create = function(elParent){
	this.el = this.doc.createElement("DIV");
		this.el.className = "thread-list-popup";
		//this.el.style.width = "560px";
		//this.el.style.backgroundColor = "white";
		this.el.style.zIndex = 5;
	if( elParent ) elParent.appendChild( this.el );
			
	var elHeader = this.doc.createElement("DIV");
		elHeader.className = "header";
		//elHeader.style.textAlign = "center";
	this.el.appendChild( elHeader );
	this.elTitle = this.doc.createElement("SPAN");
		this.elTitle.className = "title";
	elHeader.appendChild( this.elTitle );
	var elClose = this.doc.createElement("IMG");
		elClose.src = "chrome://gtdinbox/content/cross.png";
		elClose.className = "close-button";
		elClose.style.position = "absolute";
		elClose.style.right = "0px";
		elClose.style.top = "0px";
		this.eventObservers.observe(elClose, "mousedown", this.clickClose, false);
	elHeader.appendChild(elClose);
	
	this.elLoading = this.doc.createElement("DIV");
		this.elLoading.className = "loading";
		this.elLoading.style.display = "none";
	this.el.appendChild( this.elLoading );
	this.elLoading.innerHTML = "<img src='chrome://gtdinbox/skin/indicator_medium.gif'/>";
	
	this.elContent = this.doc.createElement("DIV");
	this.el.appendChild( this.elContent );
	
	this.threadActions = new ThreadActionsUI(this.gtdinboxPage);
	this.threadActions.addEventHandler(this, "click-finished", this.clickClose);
	this.labelSelect = new LabelSelectUI(this.gtdinboxPage, this.elContent, null, this.threadActions);
	this.labelSelect.addEventHandler(this, "click-label", this.toggleLabel);
	//this.labelsShowhider = new Showhider( {doc:this.doc, elParent:this.el, elContent:this.labelSelect.el, title:"Labels", show:true} );
	this.labelSelect.elActionsContainer.appendChild( this.threadActions.el );
		
	this.elPreviewBox = this.doc.createElement("DIV");
		this.elPreviewBox.className = "preview-box";
	//this.el.appendChild( this.elPreviewBox );
	this.previewShowhider = new Showhider( {doc: this.doc, elParent: this.elContent, elContent: this.elPreviewBox, title: "Preview", show: true});
	
	this.elPreview = this.doc.createElement("DIV");
		this.elPreview.className = "preview";
	this.elPreviewBox.appendChild( this.elPreview );
	/*
	var elPreviewView = this.doc.createElement("DIV");
		elPreviewView.className = "view";
		elPreviewView.textAlign = "right";
		elPreviewView.innerHTML = "view&#187;";	// TODO-LANGUAGE
	this.elPreviewBox.appendChild( elPreviewView );
	*/
	
};

ThreadListPopupUI.prototype.onPopup = function(){
	this._checked = this.threadListUI.checkRows(null, false);
};
ThreadListPopupUI.prototype.onPopupOff = function(){
	if( this._checked ){
		this.threadListUI.checkRows( this._checked );
		this._checked = null;
	}
};



ThreadListPopupUI.prototype.threadUpdate = function(){
	// TODO Only if visible
	if( !this.poppedUp() ) return;
	this.labelSelect.refresh(this.thread);
	this.threadActions.refresh(this.thread);
	//this.labelSelect.selected(this.thread.labels, true);
	this.labelSelect.newLabels( this.thread.getUnknownBodyLabels() );
	this.elPreview.innerHTML = this.thread.getLatestMsg();
};
ThreadListPopupUI.prototype.labelsUpdate = function(){
	// TODO Only if visible
try{
	if( !this.poppedUp() ) return;
	//this.threadListUI.uncheckOutOfView();	// causes problems on sent items
	this.thread.verifyLabels( this._labels.getActiveLabels() );
	this.labelSelect.refresh(this.thread);
	this.threadActions.refresh(this.thread);
}catch(e){
	if( GTDDEBUGMODE ) alert("threadlistpopupui labelsUpdate\n" + e.toString());
}
};

ThreadListPopupUI.prototype.setRow = function(row){
	// Update header, refresh labels (+ set selected), update preview
	
	try{
		this.elTitle.innerHTML = row.subject;
			
		this.thread = row.thread();
		if( this.thread ){
			this._setRow_refreshUI(this.thread, row);
		}else{
			this.retryThread(true,row);
		}

	}catch(e){
		if( GTDDEBUGMODE ) alert("popupui setRow\n" + e.toString());
	}
};
ThreadListPopupUI.prototype._setRow_refreshUI = function(thread, row){
try{
	if( !thread ) return;
	this.thread = thread;
	
	
	this.thread.initEventHandler();
	this.thread.eventHandlers.reset();	// get it exclusively for us
	
	//this.threadListUI.uncheckOutOfView();	// causes problems on sent items
	var labels = this.thread.verifyLabels( this._labels.getActiveLabels() );	// ensure it's up to date
	
		
	this.labelSelect.refresh(this.thread);
	this.labelSelect.newLabels( this.thread.getUnknownBodyLabels() );
	this.threadActions.refresh(this.thread);
		
	this.elPreview.innerHTML = this.thread.getLatestMsg();
	
	this.thread.addEventHandler(this, "addLabel", this.threadUpdate);
	this.thread.addEventHandler(this, "removeLabel", this.threadUpdate);
	this.thread.addEventHandler(this, "update-body", this.threadUpdate);
	
}catch(e){
	if( GTDDEBUGMODE ) alert("popupui _setRow_refreshUI\n" + e.toString());
}
};

ThreadListPopupUI.prototype.loading = function(v){
	if( typeof v=="undefined" ) return this.elContent.style.display=="none";
	this.elContent.style.display = v? "none" : "";
	this.elLoading.style.display = v? "" : "none";
};

ThreadListPopupUI.prototype.retryThread = function(v, row){
	if( this._retryInterval ){
		clearInterval( this._retryInterval );
		this._retryInterval = true;
	}
	if( v ){
		this.loading(true);
		var fRetry = GIPROTOTYPE.bind(this, function(){
				var thread = row.thread();
				if( thread ){
					this.retryThread(false);
					this._setRow_refreshUI(thread, row);
				}
			});
		this._retryInterval = setInterval( fRetry, 400 );
	}else{
		this.loading(false);
	}
};

ThreadListPopupUI.prototype.toggleLabel = function(e, label){
	// Inform Gmail of change
		
try{
	
	//var on = this.thread? !this.thread.hasLabel(label.labelname) : undefined;
	var labelname = label.labelname;
	var fCallback = GIPROTOTYPE.bind(this, function(act){
			if( act > 0 ) this.labelSelect.selected( this.thread.labels );
		});
	this.gtdinboxPage._labels.labelThread({labelname:label.labelname, 'on':undefined, 'threads':this.thread, 'fCallback':fCallback});
	
}catch(e){
	if( GTDDEBUGMODE ) alert("threadlistpopupui clicklabel\n" + e.toString());
}
};

ThreadListPopupUI.prototype.clickClose = function(event){
	this.retryThread(false);
	this.popupOff();
};